# 快速诊断脚本 - 简化版
# 用于检查性能数据导出问题

Write-Host "=== 快速诊断 ===" -ForegroundColor Cyan
Write-Host ""

# 步骤 1: 检查编译是否成功
Write-Host "1. 检查最新编译是否包含性能分析代码..." -ForegroundColor Yellow
if (Test-Path "entry\build\default\outputs\default\entry-default-signed.hap") {
    $hapTime = (Get-Item "entry\build\default\outputs\default\entry-default-signed.hap").LastWriteTime
    $codeTime = (Get-Item "entry\src\main\cpp\Common\PerformanceProfiler.hpp").LastWriteTime
    
    if ($hapTime -gt $codeTime) {
        Write-Host "✓ HAP 包含最新代码 (编译于 $hapTime)" -ForegroundColor Green
    } else {
        Write-Host "⚠ HAP 可能是旧版本!" -ForegroundColor Red
        Write-Host "  HAP 时间: $hapTime" -ForegroundColor Gray
        Write-Host "  代码时间: $codeTime" -ForegroundColor Gray
        Write-Host ""
        Write-Host "建议: 重新编译项目" -ForegroundColor Yellow
        Write-Host "  .\hvigorw.bat assembleHap" -ForegroundColor Cyan
        Write-Host ""
    }
} else {
    Write-Host "✗ 未找到编译的 HAP 文件" -ForegroundColor Red
    Write-Host "请先编译项目: .\hvigorw.bat assembleHap" -ForegroundColor Yellow
    Write-Host ""
}

# 步骤 2: 尝试不同的可能路径
Write-Host "2. 尝试查找性能数据文件..." -ForegroundColor Yellow
Write-Host "   (这可能需要10-15秒)" -ForegroundColor Gray
Write-Host ""

$paths = @{
    "标准路径" = "/data/app/el2/100/base/com.huawei.vulkantestapp10/haps/entry/files/"
    "备选路径1" = "/data/storage/el2/base/haps/entry/files/"
    "备选路径2" = "/data/local/tmp/"
    "备选路径3" = "/sdcard/"
}

$foundPath = $null
foreach ($name in $paths.Keys) {
    $path = $paths[$name]
    Write-Host "   检查: $name" -ForegroundColor Gray
    
    try {
        $files = hdc shell "ls $path 2>/dev/null" 2>$null
        if ($files -match "profiling") {
            Write-Host "   ✓ 在 $name 找到文件!" -ForegroundColor Green
            Write-Host "   $files" -ForegroundColor Cyan
            $foundPath = $path
            break
        }
    } catch {
        # 忽略错误
    }
}

if ($foundPath) {
    Write-Host ""
    Write-Host "✓ 找到性能数据文件!" -ForegroundColor Green
    Write-Host ""
    Write-Host "请使用以下命令拉取:" -ForegroundColor Yellow
    Write-Host "hdc file recv ${foundPath}profiling_summary.csv .\oh_profiling_summary.csv" -ForegroundColor Cyan
    Write-Host "hdc file recv ${foundPath}profiling_detailed.csv .\oh_profiling_detailed.csv" -ForegroundColor Cyan
} else {
    Write-Host ""
    Write-Host "✗ 未找到性能数据文件" -ForegroundColor Red
    Write-Host ""
    Write-Host "===原因分析===" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "最可能的原因:" -ForegroundColor Red
    Write-Host "  1. 项目未重新编译,使用的是旧代码" -ForegroundColor Gray
    Write-Host "  2. Cleanup() 函数未被调用 (应用未正常退出)" -ForegroundColor Gray
    Write-Host "  3. PerformanceProfiler::Enable(true) 未被调用" -ForegroundColor Gray
    Write-Host ""
    Write-Host "===解决方案===" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "步骤 1: 完全清理并重新编译" -ForegroundColor Cyan
    Write-Host "  .\hvigorw.bat clean" -ForegroundColor Gray
    Write-Host "  .\hvigorw.bat assembleHap" -ForegroundColor Gray
    Write-Host ""
    Write-Host "步骤 2: 重新安装应用" -ForegroundColor Cyan
    Write-Host "  hdc install -r entry\build\default\outputs\default\entry-default-signed.hap" -ForegroundColor Gray
    Write-Host ""
    Write-Host "步骤 3: 启动应用并等待 2-3 分钟" -ForegroundColor Cyan
    Write-Host "  hdc shell aa start -a EntryAbility -b com.huawei.vulkantestapp10" -ForegroundColor Gray
    Write-Host "  (在设备上操作应用 2-3 分钟)" -ForegroundColor Gray
    Write-Host ""
    Write-Host "步骤 4: 正常关闭应用 (重要!)" -ForegroundColor Cyan
    Write-Host "  hdc shell aa force-stop com.huawei.vulkantestapp10" -ForegroundColor Gray
    Write-Host ""
    Write-Host "步骤 5: 再次检查" -ForegroundColor Cyan
    Write-Host "  .\diagnose_simple.ps1" -ForegroundColor Gray
    Write-Host ""
}

Write-Host ""
Write-Host "=== 诊断完成 ===" -ForegroundColor Cyan
